﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Common.Logging;
using DotNetServiceLib;

namespace DotNetService
{
    public partial class DotNetCustomService : ServiceBase
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(DotNetCustomService));

        public DotNetCustomService()
        {
            InitializeComponent();
            log.Info("Initializing 'DotNetCustomService'");
        }

        protected override void OnStart(string[] args)
        {
            String dirRoot = System.Reflection.Assembly.GetExecutingAssembly().Location;
            dirRoot = System.IO.Path.GetDirectoryName(dirRoot);

            String contextPath = dirRoot + "\\" + ApplicationContext.FileName;
            try
            {
                ApplicationContext appContext = new ApplicationContext(contextPath);
                string returnMessage = "";
                appContext.Load(ref returnMessage);
                log.Info(returnMessage);
            }
            catch (Exception e)
            {
                log.Fatal(String.Format("Spring context was not loaded succesfully: {0}\n", e.ToString()));
                log.Fatal(String.Format("Spring context was not found in: {0}\n", contextPath));
            }
        }

        protected override void OnStop()
        {
            log.Info("'DotNetCustomService' was stopped.");
        }
    }
}
